#!/usr/bin/env python3

# Copyright (c) 2005-2014 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import os
from utils import Util
from config import Config


class Category:

    def __init__(self):
        self._productId = None
        self._bDownloadUpdate = False
        self._bDownloadDrivers = False

    def get_category_id(self):
        return "win98"

    def get_category_name(self):
        return "Microsoft Windows 98"

    def get_category_cmdline_option_description(self):
        buf = ""
        buf += '--updates: download all updates.\n'
        buf += '--drivers: download all drivers.\n'
        return buf

    def set_product_and_parse_cmdline(self, productId, argv, c, dieFunc):
        self._productId = productId

        while len(argv) > c:
            if argv[c] == "--updates":
                self._bDownloadUpdate = True
                c += 1
                continue
            elif argv[c] == "--drivers":
                self._bDownloadDrivers = True
                c += 1
                continue
            else:
                break

        return c

    def get_product_id_list(self):
        ret = []

        ret += [
            "windows-98-se.x86.en-us",                  # language: English
            "windows-98-se.x86.es",                     # language: Spanish
            "windows-98-se.x86.it",                     # language: Italian
            "windows-98-se.x86.ja",                     # language: Japanese
            "windows-98-se.x86.ko",                     # language: Korean
            "windows-98-se.x86.pt-pt",                  # language: Portuguese
            "windows-98-se.x86.ru",                     # language: Russian
            "windows-98-se.x86.sl",                     # language: Slovenian
            "windows-98-se.x86.tr",                     # language: Turkish
        ]

        ret += [
            "windows-98-se-oem.x86.cz",                 # language: Czech
            "windows-98-se-oem.x86.en-us",              # language: English
            "windows-98-se-oem.x86.de",                 # language: German
            "windows-98-se-oem.x86.fi",                 # language: Finnish
            "windows-98-se-oem.x86.hu",                 # language: Hungarian
            "windows-98-se-oem.x86.nb",                 # language: Norwegian
            "windows-98-se-oem.x86.nl",                 # language: Dutch
            "windows-98-se-oem.x86.pl",                 # language: Polish
            "windows-98-se-oem.x86.pt-br",              # language: Brazilian Portuguese
            "windows-98-se-oem.x86.sv",                 # language: Swedish
            "windows-98-se-oem.x86.zh-tw",              # language: Chinese Traditional
        ]

        return ret

    def get_product_description(self, product_id):
        ret = ""
        edition, arch, lang = product_id.split(".")

        d = {
            "windows-98-se":     "Microsoft Windows 98 SE",
            "windows-98-se-oem": "Microsoft Windows 98 SE (OEM)",
        }
        ret += d[edition]
        ret += ", "

        d = {
            "x86":    "X86",
        }
        ret += d[arch]
        ret += ", "

        d = {
            "en-us": "en-US",
            "pt-br": "pt-BR",
            "pt-pt": "pt-PT",
            "zn-tw": "zh-TW",
        }
        if lang in d:
            ret += d[lang]
        else:
            ret += lang

        return ret

    def download(self, cacheDir, destDir):
        localFile = _Win98.download(self._productId, cacheDir)
        finalFile = os.path.join(destDir, self._productId + ".iso")
        Util.copyFile(localFile, finalFile, os.getuid(), os.getgid())


class _Win98:

    # from https://winworldpc.com
    _wwpDict = {
        "windows-98-se.x86.en-us": {
            "ipfs": r"QmTTNMpQALDzioSNdDJyr94rkz5tHoAHCDa155fvHyJb4L/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20(Retail%20Full).7z",
            "hash": "fae5e0759139e7a716782266dd940d9955c68188f4d5ce4cd004a1f7cdcbce63a2e1ec3ba7c6c4b1c1db511be9b409c33ec23c410e1624feb03b8dbe9670f7e4",
        },
        "windows-98-se.x86.it": {
            "ipfs": r"QmNh9XDgj7wRyiGgRBK6E32oNyR3jwQXzB9Rjtd97z8jhP/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Italian]%20(ISO).7z",
            "hash": "02f2684b3fa8ec1360b2ef614deb9a10807e88bdf50fbf0c94e7cfbdd0049ba4b092da43aaf854bc2d4a63ac8e05f4ac818f9425e5978f6620a530e1ec22e748",
        },
        "windows-98-se.x86.ja": {
            "ipfs": r"Qmcx6kupjyeVhzDZxGDV9ejRfZHDvn9nD6w3M8CFriPoy9/Microsoft%20Windows%2098%20Second%20Edition%20[Japanese]%20(Retail).7z",
            "hash": "6c70c33f1dee8fea9a69848e8c008d0594e10519",
        },
        "windows-98-se.x86.ko": {
            "ipfs": r"QmYwiVxkcLUVJ21zc3icTaJf7sxBGTVNVvagNvguLyVNMi/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Korean].7z",
            "hash": "76b4efba67c752ddb415f2d50dcabaf17511f3cfbd4fa1a6eab5c1a8a875a1e72a4db34b052d65a1c1960cbd21de20a79c4854caf138c3fea531599b8516924b",
        },
        "windows-98-se.x86.pt-pt": {
            "ipfs": r"QmcoMbStuYpPpnK7MAwVP5nb6f9ZwmdRHKX56kbgXK31hq/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Portuguese].7z",
            "hash": "7b5263b05c846c6853caba27985602d3d5ac61c4eb8398327ee1ffc62b09dc19a3e14ce66a9860641106b756ca049f00745cd017b330cf50d797130a1ccf1890",
        },
        "windows-98-se.x86.ru": {
            "ipfs": r"QmcJh6sDnFURRSyykXHnPcQaUMf7dkKmSbpdwWeMWx8QRZ/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Russian].7z",
            "hash": "d9e638d16d1a798f9d39e2439c76c9ee2f94e341df631af3cb44f0b241c346aa40d60d9aecd5771efc26cfe08fabb550256dedf958450fc1c221f329d28bc680",
        },
        "windows-98-se.x86.sl": {
            "ipfs": r"QmVe4wjgVVhRSp9Hgnfa2EcVNuMKANMbgcsPazxgPm9MDe/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Slovenian].7z",
            "hash": "0df54b644f189e28e2158a9f9953203ab4831c2f7a8c4115d82e1e8234d63a865c93097728dbd6d5e896500c2efc5d16f813d643ced52b4bcc01a7ab9f124dc5",
        },
        "windows-98-se.x86.es": {
            "ipfs": r"QmPbSxkStSQWhkaHLEmWfuCXuybDVZPWJYUH18PzEJC8VE/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Spanish].7z",
            "hash": "7d6ea0bc92da115cd258dc4b1d8750455e9aa7e521f573eaf044aef9cece1057babcfb3da7cdc2e0e12abfedf29001d905ec2dc227e58739c5fa363c8597ac8b",
        },
        "windows-98-se.x86.tr": {
            "ipfs": r"QmQWP3LnxrQG5Me6vnFjVbp1hA3WcNaKv4h5Yi1fzYCxst/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Turkish].7z",
            "hash": "7a59f5d69280889e9734a742a86d3356c1937a11582adfbeb87371fbdfe89f8bf9576b1d80fd6d5fe0f3f06a9632b577321a5cb730fc8b325fd4ec6548b5e571",
        },
        "windows-98-se-oem.x86.en-us": {
            "ipfs": r"QmXZ9oEejNbr8gpGrZtPuFa4SSsjBgpunxyahHL53kyHRx/Microsoft%20Windows%2098%20Second%20Edition.7z",
            "hash": "b5d78b6a38b485a77c7d4a71d06a41f6201bfbd14c3bff41a551cc0455c23f9764415b0e000b50b85de5e4e3f9e992a9762bfe31ba68c036d33fc4610a0693ae",
        },
        "windows-98-se-oem.x86.cz": {
            "ipfs": r"QmNuZBoFQNiKEEmj2QD4UsQG79aTxXthXTC89KDKoVEGsF/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20(OEM)%20[Czech]%20(ISO).7z",
            "hash": "f89eef24544109fedbb78148f9d86418f58ee5d0a029ab384294b41da0efb557b9ea769b112ee86bf5833d4458698835d3a4fb36beea6363bee1976a4347d951",
        },
        "windows-98-se-oem.x86.nl": {
            "ipfs": r"Qmb6dGcF2D3goGzyd5QwdhGfQo2uaFar6ptvxLDhYKd5aZ/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Dutch]%20(OEM)%20(ISO).7z",
            "hash": "8f095532b152d1841daef3a1d2c075ffad09160c87629249946f839bdaf2cf1591eb3e7311735a94b2021131867bc728a6c52d0e4a0dc4a1c36fbc57c6e211d3",
        },
        "windows-98-se-oem.x86.fi": {
            "ipfs": r"QmZjNEb9zk2nYjvmE5rBCbAywYyphzUG9zJ6GsMAW2GJFr/Microsoft%20Windows%2098%20Second%20Edition%20[Finnish]%20(OEM)%20(ISO).7z",
            "hash": "c510c7c739dd0bb8fd1628023d39359d9c0aba51",
        },
        "windows-98-se-oem.x86.de": {
            "ipfs": r"Qmbg7ZeM3aJBGQeV3g6jUZvE6p45bzQGKXSsfv4pT9r47i/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[German]%20(ISO).7z",
            "hash": "a53863c42b889f21e093b8b3c958a4245773446709959479ec15ecc3ec48cc89e8133377ffd3f4c2d817e43d4fdab3416dbcb3ae26d9e6c737590320afd7be89",
        },
        "windows-98-se-oem.x86.hu": {
            "ipfs": r"QmSUV3JM2m3vZnckD4dYds1qyhtt5DRUUTKQmBuoP636jN/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Hungarian]%20(ISO).7z",
            "hash": "8a80b80db5aff467e8286a478045f982ff0d397bc4f0ac4fc7004f25a27d11864b304a971913844a5712848f3dc117ed36a5c176c62533144158eedca3ae9658",
        },
        "windows-98-se-oem.x86.nb": {
            "ipfs": r"QmRgwmnMhgjw5sWxidG9RrQs9LmKxjp7u9envEbz2HgvCU/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Norwegian]%20(OEM).7z",
            "hash": "76e8d1766b6622907c46dd205384e19aa45862b4070cfaf6f53988f46aad39b05b11a09e23fdd9c1192f60cfa8a5bdbd3052279a7cfd749d5c0d6618d2a04981",
        },
        "windows-98-se-oem.x86.pl": {
            "ipfs": r"QmSR7JJ8koThZi9qnzNDxAn3xkk8uSq7vHPdKNWTBJZ3mc/Microsoft%20Windows%2098%20Second%20Edition%20[Polish]%20(OEM)%20(ISO).7z",
            "hash": "f7b51b107c67e692a9fc0be55666956fed8824fe",
        },
        "windows-98-se-oem.x86.pt-br": {
            "ipfs": r"QmdVGugKoJD8xrHHRonwMcBBrSquzFtEKutx44wujXmZ6R/Microsoft%20Windows%2098%20Second%20Edition%20[Portuguese-Brazil]%20(OEM)%20(ISO).7z",
            "hash": "a5cce6352a5215ef7a420843eff5df50db68af0a",
        },
        "windows-98-se-oem.x86.sv": {
            "ipfs": r"QmUziMnCgwavWh7yweh2xBgsCrQuMj59Ugu2ExsZqpt1r2/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Swedish]%20[OEM].7z",
            "hash": "6df0d9c937058158126b80b315345f3a3cbfae06a1e62dddfab4aaff6a90773b480418ddde6a790eedf398282491ca941b3b2c8e85f4d5d1c757fafeb3a65318",
        },
        "windows-98-se-oem.x86.zh-tw": {
            "ipfs": r"QmRVZVAUyJ99b75cjYndFPn2HpxQEsANDFK2wMvTmSXt3R/Microsoft%20Windows%2098%20Second%20Edition%20(4.10.2222)%20[Trad.%20Chinese]%20(OEM)%20(ISO).7z",
            "hash": "59105c256829e61c4155df3ad9e68f42b5f6c5006bb9ec9f43c8db763a2a465958320e8c28bfdd22cd996489227d28447de86e85d7d0e6aabae39e0e234f995f",
        },
    }

    @classmethod
    def get_urls(cls, productId):
        return ["ipfs://" + cls._wwpDict[productId]["ipfs"]]

    @classmethod
    def get_digest(cls, productId):
        return cls._wwpDict[productId]["hash"]

    @classmethod
    def download(cls, productId, downloadDir):
        url = cls.get_urls(productId)[0]
        url = url.replace("ipfs://", "mirror://ipfs-gateway/ipfs/")

        fn = os.path.basename(url)
        localFile = os.path.join(downloadDir, fn)

        Util.checkAndFetchHttp(localFile, [url], cls.get_digest(productId), "sha256", Config.checksumMaxTries, Config.verboseLevel)

        return localFile
